//
//  WmcTeamMemberAlbumListVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import MJRefresh
import RxSwift
import UIKit

// MARK: - 扩展WmcTeamMemberAlbumListVC + WmcNavigatorProtocol

extension WmcTeamMemberAlbumListVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.teamClouldAlbumMemberCategory]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcTeamMemberAlbumListVM()
        if let viewModel = parms.context as? WmcTeamMemberAlbumListVM {
            vm = viewModel
        }
        let vc = WmcTeamMemberAlbumListVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcTeamCloudInviteMemberVC + WmcVMProtocol

extension WmcTeamMemberAlbumListVC: WmcVMProtocol {
    typealias VMType = WmcTeamMemberAlbumListVM
}

class WmcTeamMemberAlbumListVC: WmcBaseVC, WmcStatefulViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setupInitialViewState()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        /// 获取成员列表
        setupPlaceholders()
        getData()

        WmcNotificationManager.shared.addObserver(type: .deletePhotoSucceed) { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.getMembers()
        }
    }

    /// 设置占位视图
    /// - Returns: description
    func setupPlaceholders() {
        loadingView = WmcLoadingView()
        emptyView = currentEmptyView
        let errView = WmcErrorView()
        errView.refreshClosure = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.getData()
        }
        errorView = errView
    }
    
    func getData() -> Void {
        startLoading()
        viewModel.getMembers {[weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.endLoading(animated: true, error: nil, completion: nil)
        } failure: {[weak self] error in
            guard let weakSelf = self else { return }
            weakSelf.endLoading(animated: true, error: error, completion: nil)
        }
    }

    override func makeUI() {
        title = "成员照片"

        makeNavView(showShadow: false)
        view.addSubview(tableView)
        view.backgroundColor = UIColor.wmc_gray_F7F8FA

        tableView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(8)
            make.left.right.bottom.equalToSuperview()
        }
    }

    override func bindViewModel() {

        viewModel.members.bind { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.tableView.reloadData()
        }.disposed(by: rx.disposeBag)
    }

    private lazy var tableView: UITableView = {
        let view = UITableView(frame: CGRect.zero, style: .plain)
        view.backgroundColor = .wmc_white_FFFFFF
        view.delegate = self
        view.dataSource = self
        view.register(WmcTeamMemberAlbumListCell.self, forCellReuseIdentifier: "WmcTeamMemberAlbumListCell")
        view.separatorStyle = .none
        let emptyView = WmcTeamCloudEmptyView()
        emptyView.inviteMemebrTakePhoto.isHidden = true
        emptyView.takePhotoBtn.isHidden = true
        view.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.getMembers()
            weakSelf.tableView.mj_header?.endRefreshing()
        })
        return view
    }()

    lazy var currentEmptyView: WmcCloudAllPhotoEmptyView = {
        let view = WmcCloudAllPhotoEmptyView()
        return view
    }()
}

extension WmcTeamMemberAlbumListVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in _: UITableView) -> Int {
        return 1
    }

    func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
        return viewModel.members.value.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "WmcTeamMemberAlbumListCell", for: indexPath) as! WmcTeamMemberAlbumListCell
        if let model = viewModel.members.value.yp.object(at: indexPath.row) {
            cell.configure(model: model)
        }
        return cell
    }

    func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let model = viewModel.members.value.yp.object(at: indexPath.row) {
            let vm = WmcTeamMemberAlbumVM()
            vm.title = model.remark
            vm.member = model
            vm.type = .memberPhoto
            vm.teamAblum = viewModel.teamAblum
            vm.dataNeedUpdate = { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.viewModel.getMembers()
            }
            WmcNavigatorManager.push(.teamMemberCloudAlbum, parms: vm)?.hidesBottomBarWhenPushed = true
        }
    }
}

extension WmcTeamMemberAlbumListVC {
    func hasContent() -> Bool {
        return viewModel.members.value.count > 0
    }

    func handleErrorWhenContentAvailable(_: Error) {
        // 处理错误
    }
}
